﻿#if !WINDOWS_PHONE
using System;

namespace JGLib.Settings.Records
{
    /// <summary>
    /// Extension methods for strings (and characters).
    /// </summary>
    [System.Diagnostics.DebuggerNonUserCode]
    internal static class StringUtil
    {
        /// <summary>
        /// Gets the number of spaces at the beginning of a line of text.
        /// </summary>
        /// <returns>Indentation level of the line of text.</returns>
        public static int Indent(this string line)
        {
            return line.Length - line.TrimStart(' ').Length;
        }

        /// <summary>
        /// Splits a string at the specified "splitter" characters, and does not remove empty slices.
        /// </summary>
        /// <param name="splitters">Set of characters that split the string.</param>
        /// <returns>The set of split strings.</returns>
        public static string[] Split(this string str, params char[] splitters)
        {
            return str.Split(splitters, StringSplitOptions.None);
        }
        /// <summary>
        /// Splits a string at the specified "splitter" characters, and does not remove empty slices.
        /// </summary>
        /// <param name="splitters">Set of characters that split the string.</param>
        /// <returns>The set of split strings</returns>
        public static string[] Split(this string str, string splitters)
        {
            return str.Split(splitters.ToCharArray());
        }

        /// <summary>
        ///  Splits a string at the specified "splitter" characters, and optionally removes empty slices.
        /// </summary>
        /// <param name="trim">Whether or not to trim empty slices from the result.</param>
        /// <param name="splitters">Set of characters that split the string.</param>
        /// <returns>The set of split strings.</returns>
        public static string[] Split(this string str, bool trim, params char[] splitters)
        {
            return str.Split(splitters, trim
                ? StringSplitOptions.RemoveEmptyEntries
                : StringSplitOptions.None);
        }
        /// <summary>
        ///  Splits a string at the specified "splitter" characters, and optionally removes empty slices.
        /// </summary>
        /// <param name="trim">Whether or not to trim empty slices from the result.</param>
        /// <param name="splitters">Set of characters that split the string.</param>
        /// <returns>The set of split strings.</returns>
        public static string[] Split(this string str, bool trim, string splitters)
        {
            return str.Split(trim, splitters.ToCharArray());
        }

        /// <summary>
        /// Creates a string composed of n of the current character.
        /// </summary>
        /// <param name="n">The number of characters in the string.</param>
        /// <returns>A string with n of the current character.</returns>
        public static string Times(this char c, int n)
        {
            return new string(c, n);
        }
        /// <summary>
        /// Creates a string composed of n of the current string, appended together.
        /// </summary>
        /// <param name="n">The number of times to append the current string.</param>
        /// <returns>A string composed of n times the current string.</returns>
        public static string Times(this string str, int n)
        {
            // O(log2(n))
            if (n == 0) return "";
            else if (n == 1) return str;
            string half = str.Times(n / 2);
            if (n % 2 == 0) return half + half;
            else return half + half + str;
        }
    }
}
#endif